/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kmagsoft.jconvert.gui;

import com.kmagsoft.jconvert.utils.GuiUtils;
import com.kmagsoft.jconvert.utils.PropUtils;
import gr.anagnosg.general.utils.HashMapOrder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author anagnosg
 */
public class ParametersDialog extends javax.swing.JDialog {
    BatchConvertGui par ;
    /**
     * Creates new form ParametersDialog
     */
    public ParametersDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        par = (BatchConvertGui)parent;
        initComponents();
        init();
    }
    
    public void init(){
        //Load actions from properties file. 
         List<String> actions = PropUtils.loadParamActionValues();
         for(int i = 0 ; i < actions.size() ; i++){
            String v = actions.get(i);
            this.jActionsListModel.addElement(v);
            List paramsList = GuiUtils.findActionParams(v );
            this.model.put(v, paramsList);
         }
         this.jActionList.setModel(jActionsListModel);
         this.jParamsList.setModel(this.jParametersListModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jActionList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jParamsList = new javax.swing.JList();
        jAddActionBut = new javax.swing.JButton();
        jActionRemove = new javax.swing.JButton();
        jActionUp = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jValue = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Convert Parameters");

        jActionList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jActionList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jActionListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jActionList);

        jLabel1.setText("Actions");

        jLabel2.setText("Parameters");

        jParamsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane2.setViewportView(jParamsList);

        jAddActionBut.setText("Add");
        jAddActionBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAddActionButActionPerformed(evt);
            }
        });

        jActionRemove.setText("Remove");
        jActionRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jActionRemoveActionPerformed(evt);
            }
        });

        jActionUp.setText("Up");
        jActionUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jActionUpActionPerformed(evt);
            }
        });

        jButton4.setText("Down");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Up");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Down");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Add");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("Remove");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("Close");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setText("Save");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jLabel3.setText("Value");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jValue, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(64, 64, 64)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton8))))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jActionUp)
                                    .addComponent(jButton4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton5)
                                    .addComponent(jButton6)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton9))))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jAddActionBut)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jActionRemove)))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jActionUp, jButton4});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton10, jButton5, jButton6});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jActionUp)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton10)
                            .addComponent(jButton9))
                        .addGap(67, 67, 67))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jActionRemove)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton7)
                                .addComponent(jButton8))
                            .addComponent(jAddActionBut))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jActionRemove, jButton7, jButton8, jButton9});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jAddActionButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAddActionButActionPerformed
        // TODO add your handling code here:
        if(this.jValue.getText()!=null && !this.jValue.getText().trim().equals("")){
            if(!this.model.containsKey(this.jValue.getText().trim())){
                this.jActionsListModel.addElement(this.jValue.getText().trim());
                this.model.put(this.jValue.getText().trim(), new ArrayList());
            }
            else{
                JOptionPane.showMessageDialog(this, "This action already exists!"
                ,"Warning", JOptionPane.PLAIN_MESSAGE);
            }
        }
        else{
            JOptionPane.showMessageDialog(this, "Please Insert Value"
                ,"Warning", JOptionPane.PLAIN_MESSAGE);
        }
        
    }//GEN-LAST:event_jAddActionButActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
        ((BatchConvertGui)this.par).loadActions();
        dispose();
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jActionListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jActionListValueChanged
        // TODO add your handling code here:
        
        //System.out.println(this.jActionList.getSelectedValue());
         String selectedAction  =(String) this.jActionList.getSelectedValue();
        if(selectedAction!=null ){
            //read application properties files and fill
            List params = GuiUtils.findActionParams(selectedAction);
            this.jParametersListModel.clear();
            for(int i = 0 ; i< params.size() ; i ++){
                this.jParametersListModel.addElement(params.get(i));
            }
        }
    }//GEN-LAST:event_jActionListValueChanged

    private void jActionUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jActionUpActionPerformed
        // TODO add your handling code here:
        
        String action  = (String)this.jActionList.getSelectedValue();
        HashMapOrder hmOrder = new HashMapOrder(this.model);
        boolean canUp = hmOrder.canGoUp(action);
        int index = hmOrder.getIndex(action);
        if(canUp){
            Object val = this.jActionsListModel.getElementAt(index);
            Object previous = this.jActionsListModel.getElementAt(index-1);
            this.jActionsListModel.setElementAt(previous, index);
            this.jActionsListModel.setElementAt(val, index-1);
            hmOrder.elementUp(action);
            this.model = hmOrder.getHm();
            this.jActionList.setSelectedIndex(index-1);
        }
    }//GEN-LAST:event_jActionUpActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        if(this.jValue.getText()!=null && !this.jValue.getText().trim().equals("")){
            String action = (String)this.jActionList.getSelectedValue();
            
            if(action!=null && !action.equals("")){
                ((List)this.model.get(action)).add(this.jValue.getText().trim());
                this.jParametersListModel.addElement(this.jValue.getText().trim());
            }
            else{
                JOptionPane.showMessageDialog(this, "Please Select Action"
                ,"Warning", JOptionPane.PLAIN_MESSAGE);
            }
            
        }
        else{
            JOptionPane.showMessageDialog(this, "Please Insert Value"
                ,"Warning", JOptionPane.PLAIN_MESSAGE);
        }
        
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jActionRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jActionRemoveActionPerformed
        // TODO add your handling code here:
        if(this.jActionList.getSelectedValue()!=null ){
            String action = (String)this.jActionList.getSelectedValue();
            this.model.remove(action);
            this.jActionsListModel.removeElementAt(this.jActionList.getSelectedIndex());
        }
        else{
             JOptionPane.showMessageDialog(this, "Please select action to remove."
                ,"Warning", JOptionPane.PLAIN_MESSAGE);
        }
        
    }//GEN-LAST:event_jActionRemoveActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
        if( this.jActionList.getSelectedValue()!=null){
            if(this.jParamsList.getSelectedValue()!=null ){
                //String param = (String)this.jParamsList.getSelectedValue();
                ((List)this.model.get(this.jActionList.getSelectedValue())).remove((int)this.jParamsList.getSelectedIndex());
                this.jParametersListModel.removeElementAt(this.jParamsList.getSelectedIndex());
            }
            else{
                 JOptionPane.showMessageDialog(this, "Please select parameter to remove."
                    ,"Warning", JOptionPane.PLAIN_MESSAGE);
            }
        }
        else{
                 JOptionPane.showMessageDialog(this, "Please select action."
                    ,"Warning", JOptionPane.PLAIN_MESSAGE);
        }
        
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:
        GuiUtils.saveParamProperyFile(  model);
        
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
         String action  = (String)this.jActionList.getSelectedValue();
        HashMapOrder hmOrder = new HashMapOrder(this.model);
        boolean canDown = hmOrder.canGoDown(action);
        int index = hmOrder.getIndex(action);
        if(canDown){
            Object val = this.jActionsListModel.getElementAt(index);
            Object next= this.jActionsListModel.getElementAt(index+1);
            this.jActionsListModel.setElementAt(next, index);
            this.jActionsListModel.setElementAt(val, index+1);
            hmOrder.elementDown(action);
            this.model = hmOrder.getHm();
            this.jActionList.setSelectedIndex(index+1);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        
       
        int index = this.jParamsList.getSelectedIndex();
        if(index!=0){
            //get previous opbject 
            Object val = this.jParametersListModel.get(index);
            Object previous = this.jParametersListModel.get(index-1);
            this.jParametersListModel.set(index, previous);
            this.jParametersListModel.set(index-1, val);
            //update model .
            ((List)this.model.get(this.jActionList.getSelectedValue())).set(index, previous);
            ((List)this.model.get(this.jActionList.getSelectedValue())).set(index-1, val);
            this.jParamsList.setSelectedIndex(index-1);
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        
        int index = this.jParamsList.getSelectedIndex();
        if(index!=this.jParametersListModel.size()-1){
            //get next object
            Object val = this.jParametersListModel.get(index);
            Object next = this.jParametersListModel.get(index+1);
            this.jParametersListModel.set(index, next);
            this.jParametersListModel.set(index+1, val);
            //update model .
            ((List)this.model.get(this.jActionList.getSelectedValue())).set(index, next);
            ((List)this.model.get(this.jActionList.getSelectedValue())).set(index+1, val);
            this.jParamsList.setSelectedIndex(index+1);
        }
        
    }//GEN-LAST:event_jButton6ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ParametersDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ParametersDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ParametersDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ParametersDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ParametersDialog dialog = new ParametersDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jActionList;
    private javax.swing.JButton jActionRemove;
    private javax.swing.JButton jActionUp;
    private javax.swing.JButton jAddActionBut;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jParamsList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jValue;
    // End of variables declaration//GEN-END:variables
    private DefaultListModel jActionsListModel = new  DefaultListModel();
    private DefaultListModel jParametersListModel = new  DefaultListModel();
    private LinkedHashMap model = new LinkedHashMap();
    
}
